{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAj8AAAEGCAYAAACKMfTPAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOn0lEQVR4nO3dUWiWdd8H8P/eHmgrrE0XUpOeoQdaJBGakB4keBBioBam0UFuB0ULitZBHbSTtIOgCQV6FLMCC+lAPTClkuhgW+QGgQcpNZmURHt0U2okWNzPQS/E2+vzPP7uZ1fb7t/nczi+1/W/tv+u26/X4Po11Wq1AgCQxf/M9gUAAPyVlB8AIBXlBwBIRfkBAFJRfgCAVJQfACCVv0XC7e3ttc7OzooupZTp6elQfnx8PJRvbW0N5Usp5Y477gjlm5qawmtcr/Hx8XLhwoUZWaDqvYwaGxsL5a9evRpe48477wzlb7rppvAa12sm97KUubefV65cCeVPnz4dXmPBggWh/LJly8JrXK/5dG9evHgxlI9+zjY3N4fypZRy9913h/I+Z+sTfbXNuXPnwmvMpe+3lFJGR0cv1Gq12/789VD56ezsLCMjIzN3VX8yPDwcynd3d4fyjzzySChfSil9fX2hfD03/vVavXr1jJ2r6r2M2rp1ayg/MTERXuPNN98M5Wfy5131uefafp45cyaUf+CBB8JrPPjgg6H8oUOHwmtcr/l0b7777ruh/M6dO0P5ev7xGxwcDOV9ztYn+p+Snp6e8BoDAwPhY6rU1NR0zQbnz14AQCrKDwCQivIDAKSi/AAAq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AAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 720x360 with 10 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.datasets import load_digits\n",
    "digits = load_digits()\n",
    "\n",
    "fig, axes = plt.subplots(2, 5, figsize=(10, 5),\n",
    "                         subplot_kw={'xticks':(), 'yticks': ()})\n",
    "for ax, img in zip(axes.ravel(), digits.images):\n",
    "    ax.imshow(img,cmap=plt.cm.Greys)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "image",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m     95\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 96\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     97\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'image'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-430cfcd54de8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdigits\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimage\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m     96\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     97\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 98\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     99\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    100\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__setstate__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: image"
     ]
    }
   ],
   "source": [
    "digits.image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 8, 8)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1a520035f28>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAD4CAYAAAA0L6C7AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/d3fzzAAAACXBIWXMAAAsTAAALEwEAmpwYAAALM0lEQVR4nO3df6jV9R3H8derm2VaYr9p6maxENrYMi62EAp1G7Yi+2MwHQWLhn8VyTbCYv/snzUYRGOETbQfkKttViyi1cLsF2suf23Lroa5mndWFhGabf7I9/64R7B22/2ec76/fPd8gHTvPYfzeZ/s2ffcc7/3+3FECEAexzU9AIByETWQDFEDyRA1kAxRA8kcX8WDnuATY7wmVvHQnymHJ9f373D6tLdrW+utg5NqW+vA1sO1rVWn/2ifDsR+j3ZbJVGP10Rd7HlVPPRnyodzL65trZV33F7bWre9Ob+2tXZ9bW9ta9VpXaz51Nt4+Q0kQ9RAMkQNJEPUQDJEDSRD1EAyRA0kQ9RAMkQNJFMoatvzbW+zvd320qqHAtC7MaO2PSDpTkmXS7pA0iLbF1Q9GIDeFDlSz5K0PSJ2RMQBSQ9KWlDtWAB6VSTqKZJ2HvX5cOdrH2N7se31ttcf1P6y5gPQpSJRj/brXf9ztcKIWB4RgxExOE4n9j8ZgJ4UiXpY0rSjPp8qaVc14wDoV5GoX5J0vu1zbZ8gaaGkR6sdC0CvxrxIQkQcsn2DpCclDUi6OyK2VD4ZgJ4UuvJJRDwu6fGKZwFQAs4oA5IhaiAZogaSIWogGaIGkiFqIBmiBpKpZIeOrA5fNrPW9Z6/81e1rfXqwdqW0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TbA5L+JemRascC0KtuX37Pk/RaRLxRxTAA+tftL3QslPTAaDfYXixpsSSNZ/88oDGFj9Sda35fJel3o93OtjtAO3Tz8vtySRsj4u2qhgHQv26iXqRPeekNoD0KRW17gqRvSHq42nEA9KvotjsfSjq94lkAlIAzyoBkiBpIhqiBZIgaSIaogWSIGkiGqIFkiBpIxhFR/oPa70jq9tczz5D0bunDtEPW58bzas4XIuLM0W6oJOpe2F4fEYNNz1GFrM+N59VOvPwGkiFqIJk2Rb286QEqlPW58bxaqDXfUwMoR5uO1ABKQNRAMq2I2vZ829tsb7e9tOl5ymB7mu21todsb7F9U9Mzlcn2gO1Nth9repYy2Z5se7XtrZ2/u0uanqlbjX9P3dkg4FWNXC5pWNJLkhZFxCuNDtYn2+dIOiciNto+RdIGSVcf68/rCNs/kDQoaVJEXNn0PGWxfZ+k5yNiRecKuhMi4v2Gx+pKG47UsyRtj4gdEXFA0oOSFjQ8U98i4s2I2Nj5eK+kIUlTmp2qHLanSrpC0oqmZymT7UmSLpW0UpIi4sCxFrTUjqinSNp51OfDSvIf/xG2p0uaKWldw6OU5Q5JN0s63PAcZTtP0juS7ul8a7HC9sSmh+pWG6L2KF9L83M22ydLekjSkojY0/Q8/bJ9paTdEbGh6VkqcLykiyQti4iZkvZJOube42lD1MOSph31+VRJuxqapVS2x2kk6FURkeXyyrMlXWX7dY18qzTX9v3NjlSaYUnDEXHkFdVqjUR+TGlD1C9JOt/2uZ03JhZKerThmfpm2xr53mwoIm5vep6yRMQtETE1IqZr5O/q6Yi4puGxShERb0naaXtG50vzJB1zb2x2u0Fe6SLikO0bJD0paUDS3RGxpeGxyjBb0rWS/m57c+drt0bE482NhAJulLSqc4DZIem6hufpWuM/0gJQrja8/AZQIqIGkiFqIBmiBpIhaiAZogaSIWogmf8CC0CmOH9yAqMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "gi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.target.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=[]\n",
    "for i in range(10):\n",
    "    for j in range(1,501):\n",
    "        digit = cv2.imread('./data/%d/%d_%d.bmp' % ( i,i,j))\n",
    "        X.append(digit[::,0])\n",
    "        \n",
    "X = np.asarray(X) # numpy 对象\n",
    "y= np.array([i for i in range(10)] * 500)\n",
    "y.sort()\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0.,  0.,  5., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  0., 13., ..., 15.,  5.,  0.],\n",
       "        [ 0.,  3., 15., ..., 11.,  8.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  4., 11., ..., 12.,  7.,  0.],\n",
       "        [ 0.,  2., 14., ..., 12.,  0.,  0.],\n",
       "        [ 0.,  0.,  6., ...,  0.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0.,  0., ...,  5.,  0.,  0.],\n",
       "        [ 0.,  0.,  0., ...,  9.,  0.,  0.],\n",
       "        [ 0.,  0.,  3., ...,  6.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  0.,  1., ...,  6.,  0.,  0.],\n",
       "        [ 0.,  0.,  1., ...,  6.,  0.,  0.],\n",
       "        [ 0.,  0.,  0., ..., 10.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0.,  0., ..., 12.,  0.,  0.],\n",
       "        [ 0.,  0.,  3., ..., 14.,  0.,  0.],\n",
       "        [ 0.,  0.,  8., ..., 16.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  9., 16., ...,  0.,  0.,  0.],\n",
       "        [ 0.,  3., 13., ..., 11.,  5.,  0.],\n",
       "        [ 0.,  0.,  0., ..., 16.,  9.,  0.]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[ 0.,  0.,  1., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  0., 13., ...,  2.,  1.,  0.],\n",
       "        [ 0.,  0., 16., ..., 16.,  5.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  0., 16., ..., 15.,  0.,  0.],\n",
       "        [ 0.,  0., 15., ..., 16.,  0.,  0.],\n",
       "        [ 0.,  0.,  2., ...,  6.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0.,  2., ...,  0.,  0.,  0.],\n",
       "        [ 0.,  0., 14., ..., 15.,  1.,  0.],\n",
       "        [ 0.,  4., 16., ..., 16.,  7.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  0.,  0., ..., 16.,  2.,  0.],\n",
       "        [ 0.,  0.,  4., ..., 16.,  2.,  0.],\n",
       "        [ 0.,  0.,  5., ..., 12.,  0.,  0.]],\n",
       "\n",
       "       [[ 0.,  0., 10., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  2., 16., ...,  1.,  0.,  0.],\n",
       "        [ 0.,  0., 15., ..., 15.,  0.,  0.],\n",
       "        ...,\n",
       "        [ 0.,  4., 16., ..., 16.,  6.,  0.],\n",
       "        [ 0.,  8., 16., ..., 16.,  8.,  0.],\n",
       "        [ 0.,  1.,  8., ..., 12.,  1.,  0.]]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_digits\n",
    "digits = load_digits()\n",
    "X= digits.images\n",
    "y=digits.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 8, 8)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=X.reshape(X.shape[0],-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 64)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1347,)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(n_neighbors=63)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=63)\n",
    "knn.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 8, 3, 0, 1, 7, 6, 6, 8, 5, 5, 4, 4, 1, 8, 0, 0, 0, 9, 5, 6,\n",
       "       4, 0, 8, 3, 6, 4, 7, 4, 1, 8, 2, 9, 2, 4, 8, 3, 3, 1, 1, 2, 8, 5,\n",
       "       6, 2, 2, 6, 2, 8, 3, 1, 7, 7, 6, 2, 1, 1, 3, 2, 1, 9, 3, 8, 5, 0,\n",
       "       0, 7, 3, 7, 2, 7, 7, 4, 2, 0, 9, 5, 9, 2, 9, 6, 1, 5, 3, 2, 2, 3,\n",
       "       3, 1, 0, 6, 4, 8, 6, 3, 7, 5, 2, 7, 9, 8, 4, 5, 6, 1, 6, 0, 9, 6,\n",
       "       2, 1, 6, 2, 0, 6, 2, 4, 8, 5, 9, 6, 7, 8, 9, 2, 6, 4, 2, 3, 3, 6,\n",
       "       6, 9, 1, 8, 4, 6, 0, 0, 7, 9, 3, 8, 9, 6, 4, 1, 5, 6, 4, 5, 6, 1,\n",
       "       9, 8, 9, 2, 4, 5, 1, 9, 8, 4, 5, 8, 6, 4, 2, 1, 0, 1, 5, 8, 5, 7,\n",
       "       1, 3, 6, 4, 3, 5, 8, 5, 1, 1, 8, 1, 5, 7, 0, 8, 8, 4, 3, 7, 0, 3,\n",
       "       3, 8, 8, 2, 3, 0, 1, 1, 0, 6, 7, 2, 6, 4, 2, 0, 6, 9, 8, 6, 3, 4,\n",
       "       7, 8, 0, 6, 8, 5, 0, 0, 9, 4, 7, 8, 7, 5, 8, 0, 0, 1, 7, 5, 2, 3,\n",
       "       5, 7, 6, 6, 2, 4, 6, 6, 9, 2, 1, 5, 2, 4, 7, 1, 5, 3, 3, 2, 0, 3,\n",
       "       3, 1, 2, 0, 3, 3, 2, 1, 0, 8, 4, 7, 3, 6, 4, 1, 0, 8, 0, 8, 8, 8,\n",
       "       4, 1, 7, 4, 7, 3, 3, 8, 3, 5, 7, 3, 0, 0, 3, 7, 2, 4, 7, 1, 9, 5,\n",
       "       3, 0, 4, 3, 4, 3, 3, 0, 6, 1, 8, 4, 5, 3, 7, 7, 1, 0, 9, 7, 2, 9,\n",
       "       2, 1, 6, 4, 6, 4, 0, 7, 7, 2, 5, 3, 7, 5, 5, 3, 7, 3, 1, 7, 0, 4,\n",
       "       7, 7, 9, 2, 3, 8, 7, 0, 9, 2, 0, 0, 6, 6, 5, 7, 1, 6, 5, 7, 7, 9,\n",
       "       4, 9, 9, 3, 0, 1, 9, 3, 4, 3, 6, 5, 5, 4, 6, 5, 7, 4, 9, 3, 4, 5,\n",
       "       5, 8, 7, 3, 2, 8, 4, 1, 2, 0, 5, 7, 6, 5, 9, 7, 8, 9, 2, 6, 3, 8,\n",
       "       5, 3, 0, 0, 9, 3, 8, 3, 7, 8, 4, 5, 2, 9, 7, 7, 7, 0, 3, 6, 5, 4,\n",
       "       3, 2, 2, 5, 5, 7, 5, 7, 2, 4])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9288888888888889"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(knn.predict(X_test) == y_test).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9288888888888889"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn.score(X_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
